package cn.zshprint.serein.controller;

import cn.zshprint.serein.entity.Role;
import cn.zshprint.serein.pojo.ResultBean;
import cn.zshprint.serein.service.RoleService;
import cn.zshprint.serein.vo.RoleVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色控制器
 * (Role)表控制层
 *
 * @author makejava
 * @date 2021/08/02
 * @since 2021-08-02 17:03:36
 */
@RestController
@RequestMapping("role")
public class RoleController {
    /**
     * 角色服务
     */
    @Resource
    private RoleService roleService;

    /**
     * 选择一个
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne/{id}")
    public ResultBean<Role> selectOne(@PathVariable Integer id) {
        return new ResultBean<Role>(roleService.selectOne(id));
    }

    /**
     * 选择所有
     *
     * @return {@link ResultBean<List<Role>>}
     */
    @GetMapping("selectAll")
    public ResultBean<List<Role>> selectAll() {
        return new ResultBean<List<Role>>(roleService.selectAll());
    }

    /**
     * 保存
     *
     * @param name        的名字
     * @param description 描述
     * @return {@link ResultBean<Integer>}
     */
    @PostMapping("save")
    public ResultBean<Integer> save(@RequestParam("name") String name, @RequestParam("description") String description) {
        return new ResultBean<Integer>(roleService.save(name, description));
    }

    /**
     * 保存通过json
     *
     * @param roleVO 签证官的角色
     * @return {@link ResultBean<Integer>}
     */
    @PostMapping("saveByJson")
    public ResultBean<Integer> saveByJson(@RequestBody RoleVO roleVO) {
        return new ResultBean<Integer>(roleService.saveByJson(roleVO));
    }

    /**
     * 更新
     *
     * @param role 角色
     * @return {@link ResultBean<Role>}
     */
    @PutMapping("update")
    public ResultBean<Role> update(@RequestBody Role role) {
        return new ResultBean<Role>(roleService.update(role));
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link ResultBean<Integer>}
     */
    @DeleteMapping("delete/{id}")
    public ResultBean<Integer> delete(@PathVariable Integer id) {
        return new ResultBean<Integer>(roleService.delete(id));
    }
}